// ContentWindowsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ContentWindows.h"
#include "ContentWindowsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CContentWindowsDlg dialog

CContentWindowsDlg::CContentWindowsDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CContentWindowsDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CContentWindowsDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    m_hIcon = LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
    m_hIconSmall = (HICON) LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
}

CContentWindowsDlg::~CContentWindowsDlg()
{
    if (m_hIconSmall)
        DestroyIcon(m_hIconSmall);
}

void CContentWindowsDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CContentWindowsDlg)
    DDX_Control(pDX, IDC_SFTTREE1, m_Tree);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CContentWindowsDlg, CDialog)
    //{{AFX_MSG_MAP(CContentWindowsDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_SIZE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContentWindowsDlg message handlers

BOOL CContentWindowsDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    CRect rect;
    GetClientRect(&rect);
    m_Tree.MoveWindow(0, 0, rect.Width(), rect.Height());

    m_vTree = m_Tree.GetControlUnknown();
    ASSERT(m_vTree != NULL);

    // Flash

    // Create the dialog containing the Flash control
    if (!m_FlashDialog.Create(IDD_FLASH_DIALOG, &m_Tree) || !m_FlashDialog.HaveFlash()) {
         m_vTree->Item[3]->Cell[1]->Text = _T("Flash ActiveX control could not be instantiated - maybe Flash Player is not installed\n\n")
                                  _T("Flash Player is available at www.adobe.com.");
    } else {
         m_vTree->Item[3]->Cell[1]->Text = _T("");
         m_vTree->Item[3]->Cell[1]->AttachContentWindow((int) m_FlashDialog.m_hWnd, contentWindowSftTreeKeepSize);
    }

    // Windows Media Player

    // Create the dialog containing the Windows Media Player control
    if (!m_WMPDialog.Create(IDD_WMP_DIALOG, &m_Tree) || !m_WMPDialog.HaveWMP()) {
         m_vTree->Item[4]->Cell[1]->Text = _T("Windows Media Player control could not be instantiated - maybe Windows Media Player is not installed\n\n")
                                  _T("Windows Media Player is available at www.microsoft.com.");
    } else {
         m_vTree->Item[4]->Cell[1]->Text = _T("");
         m_vTree->Item[4]->Cell[1]->AttachContentWindow((int) m_WMPDialog.m_hWnd, contentWindowSftTreeKeepSize);
    }

    // Internet Explorer

    // Create the dialog containing the web browser control
    if (!m_IEDialog.Create(IDD_IE_DIALOG, &m_Tree) || !m_IEDialog.HaveWebBrowser()) {
         m_vTree->Item[5]->Cell[1]->Text = _T("Internet Explorer control could not be instantiated - maybe Internet Explorer is not installed\n\n")
                                  _T("Internet Explorer is available at www.microsoft.com.");
    } else {
        ISftTreeItemPtr pItem = m_vTree->Item[5];
        ISftTreeCellPtr pCell = pItem->Cell[1];
        pCell->Text = _T("Internet Explorer");
        pItem->MinHeightPix = 300; // always exactly 300 pixels
        pItem->MaxHeightPix = 300; // always exactly 300 pixels
        pCell->AttachContentWindow((int) m_IEDialog.m_hWnd, contentWindowSftTreeNone);
    }

    // Simple Dialog

    // Create the dialog
    m_SimpleDialog.Create(IDD_SIMPLEDIALOG, &m_Tree);

    m_vTree->Item[6]->Cell[1]->Text = _T("");
    m_vTree->Item[6]->Cell[1]->AttachContentWindow((int) m_SimpleDialog.m_hWnd, contentWindowSftTreeKeepSize);

    m_vTree->RowHeaders->MakeOptimal(); // Make row header width optimal, so text and pictures are not clipped horizontally.
    m_vTree->ColumnsObj->MakeOptimal(); // Make all columns optimal

    // triple the size of the last column
    m_vTree->Column[1]->WidthPix = m_vTree->Column[1]->WidthPix * 3;

    m_vTree->Items->RecalcHorizontalExtent(); // Update horizontal scrollbar

    m_vTree->Items->Current = 0; // select the first item
    m_vTree->Item[0]->Selected = VARIANT_true;

    SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CContentWindowsDlg::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CContentWindowsDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CContentWindowsDlg::OnSize(UINT nType, int cx, int cy) 
{
    CDialog::OnSize(nType, cx, cy);

    if (m_Tree.m_hWnd)
        m_Tree.MoveWindow(0, 0, cx, cy);
}

void CContentWindowsDlg::OnItemExpandedSfttree1(long ItemIndex) 
{
    // Here we make sure that the media stops playing when the control is not visible
    if (ItemIndex == 2)
        m_WMPDialog.m_pCtlWMP1->controls->play();
}

void CContentWindowsDlg::OnItemCollapsedSfttree1(long ItemIndex) 
{
    // here we make sure that the media stops playing when the control is not visible
    if (ItemIndex == 2)
        m_WMPDialog.m_pCtlWMP1->controls->pause();
}

void CContentWindowsDlg::OnItemDblClickSfttree1(long ItemIndex, short ColIndex, short AreaType, short Button, short Shift) 
{
    if (AreaType == constSftTreeColumnRes)
        m_vTree->Column[ColIndex]->MakeOptimal();	
}

BEGIN_EVENTSINK_MAP(CContentWindowsDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CContentWindowsDlg)
	ON_EVENT(CContentWindowsDlg, IDC_SFTTREE1, 42 /* ItemExpanded */, OnItemExpandedSfttree1, VTS_I4)
	ON_EVENT(CContentWindowsDlg, IDC_SFTTREE1, 41 /* ItemCollapsed */, OnItemCollapsedSfttree1, VTS_I4)
	ON_EVENT(CContentWindowsDlg, IDC_SFTTREE1, 5 /* ItemDblClick */, OnItemDblClickSfttree1, VTS_I4 VTS_I2 VTS_I2 VTS_I2 VTS_I2)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()